from datetime import date

from django.shortcuts import render, redirect

from apps.mine.models import BookInfo


# Create your views here.
def index(request):
    # 查询表中所有的数据
    books = BookInfo.objects.all()
    return render(request, 'mine/index/index.html', {'books': books})


# 添加数据
def add_book(request):
    book = BookInfo()
    book.btitle = '晓可自传11'
    book.bpub_date = date(2017, 6, 27)
    book.save()
    # 重定向跳转到首页
    return redirect('/mine/index/')


# 更新数据
def update_book(request):
    book = BookInfo()
    book.id = 1
    book.btitle = 'Happy.xiao'
    book.bpub_date = date(2017, 8, 27)
    book.save()
    return redirect('/mine/index/')


def get_one(request):
    b = BookInfo.objects.get(id=1)
    return b
